package com.ynny.Dao.imp;

import com.ynny.Entity.Students;
import com.ynny.Dao.StudentsDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

//数据持久层的Bean
@Repository("studentsDao")
public class StudentsImp implements StudentsDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public int insert(String number, String name, String grade, String clbum, String sex) {
        String sql = "insert into students(number,name,grade,clbum,sex) values(?,?,?,?,?)";
        return jdbcTemplate.update(sql,number,name,grade,clbum,sex);
    }

    @Override
    public List<Students> FindAll() {
        String sql = "select * from students";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Students.class));
    }

    @Override
    public int updateByName(String name, String number,String grade,String clbum,String sex) {
        String sql = "update students set number = ?,grade = ?,clbum = ?,sex = ? where name = ?";
        return jdbcTemplate.update(sql,number,grade,clbum,sex,name);
    }

    @Override
    public Students FindByNumber(String number) {
        String sql = "select * from students where number = ?";
        return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(Students.class),number);
    }

    @Override
    public int deleteByNumber(String number) {
        String sql = "delete from students where number = ?";
        return jdbcTemplate.update(sql,number);
    }
}
